<template>
  <el-dialog
    title="选择商品"
    width="858px"
    :visible="skuvisible"
    @close="close"
    @open="open"
  >
    <div class="search">
      <el-form :inline="true">
        <el-form-item label="商品名称：">
          <el-input
            v-model="page.skuName"
            placeholder="请输入"
            style="width: 300px"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit"
            >搜索</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <el-row v-loading="loading">
      <el-col :span="5" v-for="item in currentPageRecords" :key="item.skuId">
        <div class="item" @click="addSkuInfo(item)">
          <img
            src="@/assets/biaoji.png"
            class="top_img"
            :style="{ display: isNone(item) }"
          />
          <img :src="item.skuImage" class="img" />
          <div class="name ellipsis">{{ item.skuName }}</div>
        </div>
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
    <i
      class="el-icon-arrow-left"
      :class="{ arrow: this.page.pageIndex === 1 }"
      @click="leftClick"
    ></i>
    <i
      class="el-icon-arrow-right"
      :class="{ arrow: currentPageRecords.length < 10 }"
      @click="rightClick"
    ></i>
  </el-dialog>
</template>

<script>
import { GetSku } from '@/api/device-manager'
export default {
  name: 'SearchSku',
  props: {
    skuvisible: {
      type: Boolean,
      required: true
    }
  },
  data () {
    return {
      page: {
        pageIndex: 1,
        pageSize: 10,
        skuName: ''
      },
      currentPageRecords: [],
      loading: false,
      ChanneeListInfo: {}
    }
  },
  methods: {
    // 关闭
    close () {
      this.$emit('update:skuvisible', false)
    },
    // 搜索数据
    async onSubmit () {
      const { currentPageRecords } = await GetSku(this.page)
      this.currentPageRecords = currentPageRecords
    },
    // 加载数据
    async open () {
      this.loading = true
      const { currentPageRecords } = await GetSku(this.page)
      this.currentPageRecords = currentPageRecords
      this.loading = false
    },
    // 左
    async leftClick () {
      if (this.page.pageIndex === 1) return
      this.page.pageIndex--
      const { currentPageRecords } = await GetSku(this.page)
      this.currentPageRecords = currentPageRecords
    },
    // 右
    async rightClick () {
      if (this.currentPageRecords.length < 10) return
      this.page.pageIndex++
      const { currentPageRecords } = await GetSku(this.page)
      this.currentPageRecords = currentPageRecords
    },
    // 选择商品
    addSkuInfo (item) {
      this.ChanneeListInfo.sku = item
    },
    // 判断勾选样式
    isNone (item) {
      if (!this.ChanneeListInfo.sku) return 'none'
      if (this.ChanneeListInfo.sku.skuId === item.skuId) return ''
      return 'none'
    },
    // 确认
    submit () {
      this.$emit('addSkuList', this.ChanneeListInfo)
      this.close()
      this.$message({ type: 'success', message: '添加成功' })
    }
  }
}
</script>

<style lang="scss" scoped>
  .search {
    height: 64px;
    margin-bottom: 20px;
    .el-form {
      padding-top: 12px;
      ::v-deep .el-form-item__label {
        font-weight: normal;
      }
      .el-button--primary {
        background-color: rgb(95, 132, 255);
        border: 0;
      }
    }
  }
  .el-row {
    display: flex;
    flex-wrap: wrap;
    .el-col-5 {
      width: 20%;
      padding-left: 10px;
      padding-right: 10px;
      .item {
        position: relative;
        flex: 20%;
        width: 134px;
        height: 134px;
        padding-top: 16px;
        margin-bottom: 20px;
        background-color: #f6f7fb;
        box-shadow: 0 2px 4px 0 rgb(0 0 0 / 6%);
        border-radius: 4px;
        text-align: center;
        cursor: pointer;
        .top_img {
          // display: none;
          position: absolute;
          top: 0;
          left: 0;
        }
        .img {
          margin-bottom: 5px;
          height: 84px;
        }
        .name {
          width: 102px;
          margin: 0 auto;
        }
      }
    }
  }
  .arrow {
    color: #ccc;
    cursor: default !important;
  }
  .el-icon-arrow-left {
    position: absolute;
    top: 50%;
    left: 10px;
    transform: translateY(-20px);
    font-size: 40px;
    cursor: pointer;
  }
  .el-icon-arrow-right {
    position: absolute;
    top: 50%;
    right: 10px;
    transform: translateY(-20px);
    font-size: 40px;
    cursor: pointer;
  }
</style>
